package com.example.klinemqflink.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoField;

public class TimeRoundingUtil {

    // 获取整分钟时间戳
    public static long getWholeMinute(long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        dateTime = dateTime.with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    // 获取整 5 分钟时间戳
    public static long getWhole5Minutes(long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        int minute = dateTime.getMinute();
        minute = (minute / 5) * 5;
        dateTime = dateTime.withMinute(minute).with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    // 获取整 10 分钟时间戳
    public static long getWhole10Minutes(long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        int minute = dateTime.getMinute();
        minute = (minute / 10) * 10;
        dateTime = dateTime.withMinute(minute).with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    // 获取整 15 分钟时间戳
    public static long getWhole15Minutes(long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        int minute = dateTime.getMinute();
        minute = (minute / 15) * 15;
        dateTime = dateTime.withMinute(minute).with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    // 获取整 30 分钟时间戳
    public static long getWhole30Minutes(long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        int minute = dateTime.getMinute();
        minute = (minute / 30) * 30;
        dateTime = dateTime.withMinute(minute).with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    // 获取整 1 小时时间戳
    public static long getWholeHour(long timestamp) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        dateTime = dateTime.withMinute(0).with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.MILLI_OF_SECOND, 0);
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static long getRoundedTimestamp(long timestamp, String frequency) {
        switch (frequency) {
            case "1N":
                return getWholeMinute(timestamp);
            case "5N":
                return getWhole5Minutes(timestamp);
            case "10N":
                return getWhole10Minutes(timestamp);
            case "15N":
                return getWhole15Minutes(timestamp);
            case "30N":
                return getWhole30Minutes(timestamp);
            case "1H":
                return getWholeHour(timestamp);
            default:
                throw new IllegalArgumentException("不支持的时间间隔类型: " + frequency);
        }
    }
}
